package doantotnghiep.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.CategoryBusiness;
import doantotnghiep.business.MeasureUnitBusiness;
import doantotnghiep.business.MedicamentBusiness;
import doantotnghiep.business.ProviderBusiness;
import doantotnghiep.model.Category;
import doantotnghiep.model.MeasureUnit;
import doantotnghiep.model.Medicament;
import doantotnghiep.model.Provider;
import doantotnghiep.modelDTO.CategoryDTO;
import doantotnghiep.modelDTO.MeasureUnitDTO;
import doantotnghiep.modelDTO.MedicamentDTO;
import doantotnghiep.modelDTO.ProviderDTO;

/**				
* MedicamentManagController			
*				
* Version 1.0				
*				
* Date: 07-04-2014			
*				
* Copyright 				
*				
* Modification Logs:				
* DATE                 AUTHOR          DESCRIPTION				
* -----------------------------------------------------------------------				
* 07-04-2014	        TamNV            Create				
*/			
@Controller
public class MedicamentManagController {

	@Autowired
	private MedicamentBusiness medicamentBusiness;
	
	@Autowired
	private CategoryBusiness categoryBusiness;
	
	@Autowired
	private MeasureUnitBusiness measureUnitBusiness;
	
	@Autowired
	private ProviderBusiness providerBusiness;
	
	private MeasureUnit measureUnit;
	
	private Category category;
	
	private CategoryDTO categoryDTO;
	
	private MeasureUnitDTO measureUnitDTO;
	
	private Medicament medicament;
	
	private MedicamentDTO medicamentDTO;
	
	private Provider provider;
	private ProviderDTO providerDTO;
 
	@ModelAttribute("MedicamentDTO")
	public MedicamentDTO getMedicamentObject() {
		return new MedicamentDTO();
	}
    
	/*receive require view all medicament from client*/
	@RequestMapping(value = "/viewMedicament", method = RequestMethod.GET)
	public ModelAndView viewAllMedicament() {
		List<Medicament> medicaments;
		List<MedicamentDTO> medicamentDTOs = new ArrayList<MedicamentDTO>() ;
		if(medicamentBusiness != null){
			
			//get a list of all medicament from database
			medicaments = medicamentBusiness.listMedicament();
			if(medicaments != null){
				
				//transfering to MedicamentDTO
				for(Medicament me: medicaments){
					medicamentDTO = new MedicamentDTO();
					
					medicamentDTO.setId(me.getId());
					medicamentDTO.setMedicamentName(me.getMedicamentName());
					medicamentDTO.setBoughtPrice((int)me.getBoughtPrice());
					medicamentDTO.setSellPrice((int)me.getSellPrice());
					
					medicamentDTO.setCategoryName(categoryBusiness.getCategoryrById(me.getCategory().getId()).getCategoryName());
					medicamentDTO.setMeasureName(measureUnitBusiness.getMeasureById(me.getMeasure().getId()).getUnitName());
					medicamentDTO.setProviderName(providerBusiness.getProviderById(me.getProvider().getId()).getFullName());					
					medicamentDTOs.add(medicamentDTO);
				}
				
				//put data into a Map Object  to return
				Map<String, Object> listAllMedicament = new HashMap<String, Object>();
				listAllMedicament.put("medicaments", medicamentDTOs);
				return new ModelAndView("viewMedicament", "listAllMedicament",listAllMedicament);
			}
		}
		return new ModelAndView("viewMedicament","",null);
	}

	/*receive require add a new medicament from client after them entered all input*/
	@RequestMapping(value = "/addMedicament", method = RequestMethod.POST)
	public String addMedicament(HttpServletRequest request, ModelMap model,
			@ModelAttribute("MedicamentDTO") MedicamentDTO medicamentDTO) {
		if (medicamentDTO != null) {
			medicament = new Medicament();
			medicament.setMedicamentName(medicamentDTO.getMedicamentName());
			medicament.setBoughtPrice(medicamentDTO.getBoughtPrice());
			medicament.setSellPrice(medicamentDTO.getSellPrice());
			
			category = new Category();
			category.setId(medicamentDTO.getCategory_ID());
			medicament.setCategory(category);
			
			measureUnit = new MeasureUnit();
			measureUnit.setId(medicamentDTO.getMeasure_ID());
			medicament.setMeasure(measureUnit);
			
			provider = new Provider();
			provider.setId(medicamentDTO.getProvider_ID());
			medicament.setProvider(provider);
			
			this.medicamentBusiness.addMedicament(medicament);
			this.categoryBusiness.inceaseNumberOfMedicine(medicamentDTO.getCategory_ID());
			
		} 
		return "redirect:/viewMedicament";
	}

	/*receive require add a new medicament from client*/
	@RequestMapping(value = "/addMedicament", method = RequestMethod.GET)
	public ModelAndView addMedicament() {
		
		List<MeasureUnit> measureUnits ;
		List<MeasureUnitDTO> measureUnitDTOs = new ArrayList<MeasureUnitDTO>();
		List<Category> categories ;
		List<CategoryDTO> categoryDTOs = new ArrayList<CategoryDTO>();
		List<Provider> providers;
		List<ProviderDTO> providerDTOs = new ArrayList<ProviderDTO>();
		
		measureUnits = measureUnitBusiness.getlistMeasureUnit();
		categories = categoryBusiness.listCategory();
		providers = providerBusiness.listProvider();
		
		for(MeasureUnit m : measureUnits){
			measureUnitDTO = new MeasureUnitDTO();
			measureUnitDTO.setId(m.getId());
			measureUnitDTO.setUnitName(m.getUnitName());
			measureUnitDTOs.add(measureUnitDTO);
		}
		
		for(Category c: categories){
			categoryDTO =  new CategoryDTO();
			categoryDTO.setId(c.getId());
			categoryDTO.setCategoryName(c.getCategoryName());
			categoryDTOs.add(categoryDTO);
		}
		
		for(Provider pr: providers){
			providerDTO = new ProviderDTO();
			providerDTO.setId(pr.getId());
			providerDTO.setFullName(pr.getFullName());
			providerDTOs.add(providerDTO);
		}
		
		Map<String, Object> medicamentModel = new HashMap<String, Object>();
		medicamentModel.put("measures", measureUnitDTOs);
		medicamentModel.put("categories",categoryDTOs);
		medicamentModel.put("providers", providerDTOs);
		return new ModelAndView("addMedicament", "medicamentModel",medicamentModel);
	}
	
	/*receive require update information of a staff from client*/
	@RequestMapping(value = "/editMedicament", method = RequestMethod.GET)
	public ModelAndView editMedicament(HttpServletRequest request, ModelMap model,int id) {
		medicament = medicamentBusiness.getMedicamentById(id);
		if(medicament != null){
			medicamentDTO = new MedicamentDTO();
			
			medicamentDTO.setId(medicament.getId());
			medicamentDTO.setBoughtPrice((int)medicament.getBoughtPrice());
			medicamentDTO.setSellPrice((int)medicament.getSellPrice());
			medicamentDTO.setMedicamentName(medicament.getMedicamentName());
			
			List<MeasureUnit> measureUnits ;
			List<MeasureUnitDTO> measureUnitDTOs = new ArrayList<MeasureUnitDTO>();
			List<Category> categories ;
			List<CategoryDTO> categoryDTOs = new ArrayList<CategoryDTO>();
			List<Provider> providers;
			List<ProviderDTO> providerDTOs = new ArrayList<ProviderDTO>();
			
			measureUnits = measureUnitBusiness.getlistMeasureUnit();
			categories = categoryBusiness.listCategory();
			providers = providerBusiness.listProvider();
			for(MeasureUnit m : measureUnits){
				measureUnitDTO = new MeasureUnitDTO();
				measureUnitDTO.setId(m.getId());
				measureUnitDTO.setUnitName(m.getUnitName());
				measureUnitDTOs.add(measureUnitDTO);
			}
			
			for(Category c: categories){
				categoryDTO =  new CategoryDTO();
				categoryDTO.setId(c.getId());
				categoryDTO.setCategoryName(c.getCategoryName());
				categoryDTOs.add(categoryDTO);
			}
			
			for(Provider pr: providers){
				providerDTO = new ProviderDTO();
				providerDTO.setId(pr.getId());
				providerDTO.setFullName(pr.getFullName());
				providerDTOs.add(providerDTO);
			}
			
			Map<String, Object> medicamentModel = new HashMap<String, Object>();
			medicamentModel.put("measure", measureUnitDTOs);
			medicamentModel.put("category",categoryDTOs);
			medicamentModel.put("providers", providerDTOs);
			medicamentModel.put("medicamentDTO",medicamentDTO);
		  
		   return new ModelAndView("updateMedicament", "medicamentModel", medicamentModel);
		}
		return new ModelAndView("viewMedicament","",null);
	}

    /*receive require update infor of a staff from client after them entered all input*/
	@RequestMapping(value = "/editMedicament", method = RequestMethod.POST)
	public String editMedicament(HttpServletRequest request, ModelMap model,
			@ModelAttribute("MedicamentDTO") MedicamentDTO medicamentDTO) {
		if (medicament != null) {
			if (medicamentDTO != null) {
				medicament = new Medicament();
				
				medicament.setId(medicamentDTO.getId());
				medicament.setMedicamentName(medicamentDTO.getMedicamentName());
				medicament.setBoughtPrice(medicamentDTO.getBoughtPrice());
				medicament.setSellPrice(medicamentDTO.getSellPrice());
				
				category = new Category();
				category.setId(medicamentDTO.getCategory_ID());
				medicament.setCategory(category);
				
				measureUnit = new MeasureUnit();
				measureUnit.setId(medicamentDTO.getMeasure_ID());
				medicament.setMeasure(measureUnit);
				
				provider = new Provider();
				provider.setId(medicamentDTO.getProvider_ID());
				medicament.setProvider(provider);
				
				this.medicamentBusiness.addMedicament(medicament);
			} 
			this.medicamentBusiness.saveMedicamentChanged(medicament);
		}
		return "redirect:/viewMedicament";
	}

	/*receive require delete a staff from database*/
	@RequestMapping(value = "/deleteMedicament", method = RequestMethod.GET)
	public String deleteMedicament(HttpServletRequest request, ModelMap model,
			  int id) {
		medicamentBusiness.deleteMedicament(id);
		return "redirect:/viewMedicament";
	}
	
	/*receive require search a medicament from database*/
	@RequestMapping(value = "/searchMedicament", method = RequestMethod.GET)
	public ModelAndView searchStaff(HttpServletRequest request, ModelMap model,
			  String key) {
		List<Medicament> medicaments;
		Map<String, Object> medicamentModel = new HashMap<String, Object>();
		List<MedicamentDTO> medicamentDTOs = new ArrayList<MedicamentDTO>();
		medicaments = medicamentBusiness.getMedicamentByKeyWord(key);

		if(medicaments!=null){

			for(Medicament medicament: medicaments){
			MedicamentDTO medicamentDTO = new  MedicamentDTO();	
			
			medicamentDTO.setId(medicament.getId());
			medicamentDTO.setSellPrice((int)medicament.getSellPrice());
			medicamentDTO.setBoughtPrice((int)medicament.getBoughtPrice());
			medicamentDTO.setCategoryName(medicament.getCategory().getCategoryName());
			medicamentDTO.setMeasureName(medicament.getMeasure().getUnitName());
			medicamentDTO.setMedicamentName(medicament.getMedicamentName());
			medicamentDTO.setTotalOfBatch(medicament.getTotalOfBatch());
			medicamentDTO.setProviderName(medicament.getProvider().getFullName());	
			
			medicamentDTOs.add(medicamentDTO);
		}
			medicamentModel.put("medicamentDTOs",medicamentDTOs);
			
		}
		return new ModelAndView("medicamentSearchResult","medicamentModel",medicamentModel);
	}

}
